package com.etoak.car.controller;


import com.etoak.car.bean.Comment;
import com.etoak.car.bean.vo.CommentReq;
import com.etoak.car.bean.vo.CommentResp;
import com.etoak.car.service.CommentService;
import com.etoak.commons.enums.ETEnums;
import com.etoak.commons.response.ETResponse;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    CommentService commentService;

    @RequestMapping("/userCommentAdd")
    public ETResponse userCommentAdd(int userid,int carid,String context){

        ETResponse response = new ETResponse();

        Comment comment1 = commentService.isFirst(userid,carid);
        if (!ObjectUtils.isEmpty(comment1)){
            response.setCode(ETEnums.ERROR.getCode());
            response.setMsg(ETEnums.ERROR.getMsg());
            response.setData("您已经评论了");
            return response;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long time = System.currentTimeMillis();
        String date = sdf.format(time);
        Comment comment = new Comment();
        comment.setPingdate(date);
        comment.setContext(context);

        comment.setCarid(carid);
        comment.setUserid(userid);
        commentService.userCommentAdd(comment);


        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData("评论成功");
        return response;
    }

    @GetMapping("/getAllComment")
    public ETResponse getAllComment(){

        List<CommentResp> respList = commentService.getAllComment();

        ETResponse response = new ETResponse();

        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData(respList);
        return response;
    }

    @PostMapping("/huiComment")
    public ETResponse huiComment(@RequestBody CommentReq comment){

        int flag = commentService.huiComment(comment);
        ETResponse response = new ETResponse();
        if (flag<0){
            response.setCode(ETEnums.ERROR.getCode());
            response.setMsg(ETEnums.ERROR.getMsg());
            response.setData("回复失败");
        }
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData("回复成功");
        return response;
    }

    @GetMapping("/delComment/{id}")
    public ETResponse delComment(@PathVariable int id){

        int flag = commentService.delComment(id);

        ETResponse response = new ETResponse();
        if (flag<0){
            response.setCode(ETEnums.ERROR.getCode());
            response.setMsg(ETEnums.ERROR.getMsg());
            response.setData("删除失败");
        }
        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData("删除成功");
        return response;
    }

    @GetMapping("/querySomeComment/{id}")
    public ETResponse querySomeComment(@PathVariable int id){
        List<CommentResp> respList = commentService.querySomeComment(id);

        ETResponse response = new ETResponse();

        response.setCode(ETEnums.SUCCESS.getCode());
        response.setMsg(ETEnums.SUCCESS.getMsg());
        response.setData(respList);
        return response;
    }
}
